package com.gitee.ixtf.vertx

import com.gitee.ixtf.core.exception.JError
import io.github.classgraph.ClassGraph
import io.github.classgraph.ClassInfoList
import io.github.classgraph.ScanResult
import io.vertx.core.Expectation
import io.vertx.core.http.HttpResponseExpectation
import io.vertx.ext.web.client.HttpResponse
import jakarta.validation.ConstraintViolationException

object ScSuccess : Expectation<HttpResponse<*>> {
  override fun test(res: HttpResponse<*>?): Boolean = HttpResponseExpectation.SC_SUCCESS.test(res)

  override fun describe(value: HttpResponse<*>?): Throwable? {
    return RuntimeException(value?.bodyAsString())
  }
}

fun classInfoList(
    vararg packages: Class<*>,
    block: ScanResult.() -> Iterable<ClassInfoList>
): Collection<Class<*>> =
    ClassGraph()
        .acceptPackages(*packages.map { it.packageName }.toTypedArray())
        .enableAllInfo()
        .scan()
        .use { scanResult -> block(scanResult).flatMap { it.loadClasses() } }
        .distinct()

fun nonLogT(t: Throwable) =
    t is JError || t is ConstraintViolationException || t is graphql.AssertException
